#coding=utf-8
'''
Created on Dec 10, 2015
@author: DQA wangpeng 
'''
import data.desired_setting

import data.logging_save

import time
from appium import webdriver
import data.config
import data.get_status
import threading
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import os
import sys
from time import ctime
from cookielib import logger
reload(sys)
sys.setdefaultencoding('utf-8')

import data.logging_save
import data.Create_Log_Folder
import unittest

class guess(unittest.TestCase):
    def setUp(self):
        #startup the huawei m130 app
        self.save_log = data.Create_Log_Folder.Create('xiami_popular_commend')
        desired_caps = data.desired_setting.desired_setting()
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        data.logging_save.logging.info("startup the app already")
        print 'startup the app already'
        
    def tearDown(self):
        self.driver.quit()
        
    def test_alert(self):
        u"""测试步骤：进入首页，进去到HOME主菜单，然后反复进入app       测试依据：进入到app之后检测底部播放器是否存在，如存在则判断正常进入到这个app"""
        #excute the test case times
        playback_times = 1
        success_times = 1
        fail_times = 1
        savenumber = 1
        save_pic = os.path.join(self.save_log+str(savenumber)+'.png')
        print save_pic
        action = TouchAction(self.driver)
        alert_number = 1
        while(alert_number <=10 and playback_times<=10):
            try:
                WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.XPATH,data.config.alert_popup())))     
                alert = self.driver.switch_to_alert()
                alert.accept()
                alert_number = alert_number +1
                
                if alert_number == 10:
                    data.logging_save.logging.info("网络环境太差，一直弹出来“网络错误提示，”无法继续测试")
                    print time.ctime(),"网络环境太差，一直弹出来“网络错误提示，”无法继续测试"
                    self.driver.get_screenshot_as_file(save_pic)
                    savenumber = savenumber + 1
                    self.fail("the network envoiroment is too bad")
                        
            except:
                
                self.driver.close_app()
                playback_times = playback_times + 1
                print playback_times
                self.driver.launch_app()
                time.sleep(5)
                #self.assertEqual("//UIAApplication[1]/UIAWindow[2]/UIAScrollView[2]/UIAStaticText[1]", data.config.playback_button_songname())
                
                if WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.XPATH,data.config.playback_button_songname()))):   
                    print 'successfully startup'
                    data.logging_save.logging.info("successfully startup")
                
                else:
                    self.fail("fail,can not startup normally")
                
        print 'finished the test'    
if __name__ == '__main__':
    unittest.main()
    print 'finished'